package com.tao.plan.common.serializer;
import java.io.IOException;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeParseException;

import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
public class LocalDateTimeDeserializer extends JsonDeserializer<LocalDateTime> {

	private static final DateTimeFormatter FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd");

	@Override
	public LocalDateTime deserialize(JsonParser p, DeserializationContext ctxt)
			throws IOException, JsonProcessingException {
		String dateString = p.getText().trim();
		try {
			LocalDate localDate = LocalDate.parse(dateString, FORMATTER);
			// 假设默认时间为午夜00:00
			LocalTime defaultTime = LocalTime.MIDNIGHT;
			return LocalDateTime.of(localDate, defaultTime);
		} catch (DateTimeParseException e) {
			throw new RuntimeException("日期字符串格式错误: " + dateString, e);
		}
	}
}
